package fr.umlv.m2.jee.pmlv.persistence.planning.dao;

import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.stereotype.Repository;

import fr.umlv.m2.jee.pmlv.framework.dao.hibernate.AbstractHibernateDAO;
import fr.umlv.m2.jee.pmlv.persistence.planning.AbstractEvent;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * DAO for the class {@link AbstractEvent with the framework Hibernate.
 * This class implements {@link IAbstractEventDAO}.
 * @author jenseric
 *
 */
@Repository("hibernateAbstractEventDAO")
public class HibernateAbstractEventDAO extends AbstractHibernateDAO<Long, AbstractEvent> implements IAbstractEventDAO {

    @Override
    public List<AbstractEvent> findByWeekAndByProfessorUidAndByYear(GregorianCalendar start, String uid, int year) {
        GregorianCalendar tmp = (GregorianCalendar)start.clone();
        List<AbstractEvent>  list = new ArrayList<AbstractEvent>();
        for(int i = 0;i < 7 ;i ++) {
                String query = "select distinct event from AbstractEvent as event, UserSubject as us"
                        + " where us.primaryKey.uid='"+uid+"' and us.primaryKey.year="+year+" and"
                        + " us.primaryKey.subject.id = event.subject.id and"
                        + " year(event.begin)="+tmp.get(Calendar.YEAR)+" and month(event.begin)="+(tmp.get(Calendar.MONTH)+1)+"and day(event.begin)="+tmp.get(Calendar.DATE);
                tmp.add(Calendar.DATE, 1);
                list.addAll(findByQuery(query));
        }
        return list;
    }

    @Override
    public List<AbstractEvent> findByWeekAndByStudentUidAndByYear(GregorianCalendar start, String uid, int year) {
        GregorianCalendar tmp = (GregorianCalendar)start.clone();
        List<AbstractEvent>  list = new ArrayList<AbstractEvent>();
        for(int i = 0;i < 7 ;i ++) {
                String query = "select distinct event from AbstractEvent as event, UserSpecialty as us, SpecialtySubject as spe"
                        + " where us.primaryKey.uid='"+uid+"' and us.primaryKey.year="+year+" and"
                        + " us.primaryKey.specialty.id = spe.specialty.id and spe.subject.id = event.subject.id and"
                        + " year(event.begin)="+tmp.get(Calendar.YEAR)+" and month(event.begin)="+(tmp.get(Calendar.MONTH)+1)+"and day(event.begin)="+tmp.get(Calendar.DATE);
                tmp.add(Calendar.DATE, 1);
                list.addAll(findByQuery(query));
        }
        return list;
    }

    @Override
    public List<AbstractEvent> findByWeekAndBySecretaryUidAndByYear(GregorianCalendar start, String uid, int year) {
        GregorianCalendar tmp = (GregorianCalendar)start.clone();
        List<AbstractEvent>  list = new ArrayList<AbstractEvent>();
        for(int i = 0;i < 7 ;i ++) {
                String query = "select distinct event from AbstractEvent as event, UserCursus as us, SpecialtySubject as spe"
                        + " where us.primaryKey.uid='"+uid+"' and us.primaryKey.year="+year+" and"
                        + " us.primaryKey.cursus.id = spe.specialty.cursus.id and spe.subject.id = event.subject.id and"
                        + " year(event.begin)="+tmp.get(Calendar.YEAR)+" and month(event.begin)="+(tmp.get(Calendar.MONTH)+1)+"and day(event.begin)="+tmp.get(Calendar.DATE);
                tmp.add(Calendar.DATE, 1);
                list.addAll(findByQuery(query));
        }
        return list;
    }
}



